#include"compile_run.hpp"
#include"../common/httplib.h"

using namespace ns_compile_and_run;
using namespace httplib;

void Usage(std::string proc)
{
std::cerr << "Usage: " << "\n\t" << proc << " port" << std::endl;
}
// ./compile prot
int main(int argc,char *argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return-1;
    }
    Server ser;
    
    //注册服务
    ser.Post("/compile_and_run",[](const Request &req,Response &resp){
        std::string in_json = req.body;//body中存储的是json数据
        std::string out_json;
        if(!in_json.empty())
        {
            CompileAndRun::Stat(in_json,&out_json);
            resp.set_content(out_json,"application/json;charset=utf-8");
        }
    });

    ser.listen("0.0.0.0",atoi(argv[1]));//启动服务
    return 0;
}






















// //测试代码
// int main()
// {
//     //in_json: {"code": "#include...", "input": "","cpu_limit":1, "mem_limit":10240}
//     //out_json: {"status":"0", "reason":"","stdout":"","stderr":"",}
//     std::string in_json;
//     std::string out_json;
//     Json::Value in_value;
//     //R"()"" raw string
//     in_value["code"] = R"(#include<iostream>
//     int main()
//     {
//         std::cout<<"你可以看见我了"<<std::endl;
//         return 0;
//     }
//     )";
//     in_value["input"] = "";
//     in_value["cpu_limit"] = 1;
//     in_value["mem_limit"] = 10240;
//     Json::StyledWriter writer;
//     in_json = writer.write(in_value);

//     std::cout<<in_json<<std::endl;

//     CompileAndRun::Stat(in_json,&out_json);

//     std::cout<<out_json<<std::endl;

//     return 0;
// }